package com.abc;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.ScaleAnimation;
import android.widget.ImageView;

public class MyRollingView extends ImageView{

    
    Drawable drawable1;
    Drawable drawable2;
    
    Animation anim_zoom_in;
    Animation anim_zoom_out;
    public MyRollingView(Context context,Drawable drawable1,Drawable drawable2) {
	super(context);
	this.drawable1=drawable1;
	this.drawable2=drawable2;
	init();
    }

    public MyRollingView(Context context, AttributeSet attrs) {
	super(context, attrs);
	TypedArray typeArray = context.obtainStyledAttributes(attrs,R.styleable.MyRolling);
	this.drawable1=typeArray.getDrawable(R.styleable.MyRolling_rolling_bgone);
	this.drawable2=typeArray.getDrawable(R.styleable.MyRolling_rolling_bgtwo);
	init();
    }
    
    private void init()
    {
	this.setBackgroundDrawable(drawable1);
	
	anim_zoom_out=new ScaleAnimation(1.0f, 0.0f, 1.0f, 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
	anim_zoom_out.setDuration(500);
	anim_zoom_out.setFillAfter(true);
	
	anim_zoom_in=new ScaleAnimation(0f, 1.0f, 1.0f, 1.0f,Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
	anim_zoom_in.setDuration(500);
	anim_zoom_in.setFillAfter(true);
    }
    
    public void flip()
    {
	this.startAnimation(anim_zoom_out);
	anim_zoom_out.setAnimationListener(new AnimationListener() {
	    
	    @Override
	    public void onAnimationStart(Animation animation) {
		// TODO Auto-generated method stub
		
	    }	    
	    @Override
	    public void onAnimationRepeat(Animation animation) {
		// TODO Auto-generated method stub
		
	    }
	    @Override
	    public void onAnimationEnd(Animation animation) {
		// TODO Auto-generated method stub
		if(MyRollingView.this.getBackground().getCurrent().getConstantState().equals(drawable1.getCurrent().getConstantState()))
		{
		    MyRollingView.this.setBackgroundDrawable(drawable2);
		}
		else
		{
		    MyRollingView.this.setBackgroundDrawable(drawable1);
		}
		
		MyRollingView.this.startAnimation(anim_zoom_in);
	    }
	});
    }
    
}
